package com.example.project.Controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.example.project.Constant.UserConstant;
import com.example.project.Entity.Result;
import com.example.project.Entity.User;
import com.example.project.Entity.UserVo;
import com.example.project.Service.BillService;
import com.example.project.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

@RestController
@RequestMapping("/User")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    BillService billService;

    //    @Autowired
//    JWT jwt;
    @RequestMapping("/getCode")
    public void getCode(HttpServletResponse resp ) throws IOException {
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(70, 24, 4, 5);
        //得到code
        String code = captcha.getCode();
        System.out.println(code);
        userService.setCode(code);
        ServletOutputStream outputStream = resp.getOutputStream();
        captcha.write(outputStream);
        outputStream.close();

    }
    @PostMapping("/login")
    public Result Login(@RequestBody UserVo user ) {
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        String code = userService.getCode();
        if(!code.equals(user.getCode())){
            return Result.error(UserConstant.User_Code_Error);
        }
        User user2=new User();
        user2.setUsername(user.getUsername());
        user2.setPassword(user.getPassword());
        User user1 = userService.QueryByName(user2);
        if (user1 != null) {
            //HashMap<String, Object> payload = new HashMap<>();
            //payload.put("id", user1.getId());
            //payload.put("username", user1.getUsername());
            // String token = jwt.setjwt(payload);
            //  System.out.println(token);
            return Result.success();
        } else {
            return Result.error(UserConstant.User_Login_Fail);
        }
    }
    @PostMapping("/register")
    public Result re(@RequestBody User user) {
        User user1 = new User();
        user1.setUsername(user.getUsername());
        user1.setPassword(null);
        User us = userService.SelectSameName(user1);
        if (us == null) {
            user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
            userService.UserAdd(user);
            return Result.success();
        } else {
            return Result.error(UserConstant.User_Name_Exist);
        }
    }
}
